<template>
  <a-card :bordered='false'>
    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='searchQuery'>
        <a-row :gutter='24'>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <a-form-item label='问题编号'>
              <a-input placeholder='请输入问题编号' v-model='queryParam.questionCode'></a-input>
            </a-form-item>
          </a-col>
<!--          <a-col :xl='6' :lg='7' :md='8' :sm='24'>-->
<!--            <a-form-item label='处理方式'>-->
<!--              <j-dict-select-tag placeholder='请选择处理方式' v-model='queryParam.dealType'-->
<!--                                 dictCode='deal_function' />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="问题状态">
              <j-dict-select-tag placeholder="请选择问题状态" v-model="queryParam.questionState" dictCode="question_state"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="问题类型">
              <j-dict-select-tag placeholder="请选择问题类型" v-model="queryParam.questionType" dictCode="question_type"/>
            </a-form-item>
          </a-col>
          <template v-if='toggleSearchStatus'>
            <a-col :xl='6' :lg='7' :md='8' :sm='24'>
              <a-form-item label='提出人'>
                <a-input placeholder='请输入提出人' v-model='queryParam.proposePersion'></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <span style='float: left;overflow: hidden;' class='table-page-search-submitButtons'>
              <a-button type='primary' @click='searchQuery' icon='search'>查询</a-button>
              <a-button type='primary' @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
              <a @click='handleToggleSearch' style='margin-left: 8px'>
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class='table-operator'>
      <a-button @click='handleAdd' type='primary' icon='plus'>新增</a-button>
      <a-button type='primary' icon='download' @click="handleExportXls('问题总览')">导出</a-button>
      <a-upload name='file' :showUploadList='false' :multiple='false' :headers='tokenHeader' :action='importExcelUrl'
                @change='handleImportExcel'>
        <a-button type='primary' icon='import'>导入</a-button>
      </a-upload>
      <a-dropdown v-if='selectedRowKeys.length > 0'>
        <a-menu slot='overlay'>
          <a-menu-item key='1' @click='batchDel'>
            <a-icon type='delete' />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style='margin-left: 8px'> 批量操作
          <a-icon type='down' />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择 <a
        style='font-weight: 600'>{{ selectedRowKeys.length }}</a>项
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

      <a-table
        ref='table'
        size='middle'
        :scroll='{x:true}'
        bordered
        rowKey='id'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}'
        class='j-table-force-nowrap'
        @change='handleTableChange'
        :customRow='clickThenCheck'>
        <template slot='bpmStatus_dictText' slot-scope='text, record'>
          <BpmStatusTagComponent :record='record' :text='text'></BpmStatusTagComponent>
        </template>
        <template slot='taskDes' slot-scope='text'>
          <j-ellipsis :value='text' :length='10' />
        </template>
        <template slot='subjectName' slot-scope='text, record'>
          <a @click='handleDetail(record)'>{{ text }}</a>
        </template>
        <span slot='action' slot-scope='text, record'>
          <a @click='handleEdit(record)' v-if='record.bpmStatus==="1"'>编辑</a>
          <a @click='confimor(record)' v-if="!record.confirmorId && record.bpmStatus == '3'">确认</a>

          <a-divider type='vertical' v-if='record.bpmStatus==="1"||record.bpmStatus==="3"&&!record.confirmorId' />
          <a-dropdown>
            <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
            <a-menu slot='overlay'>
              <a-menu-item v-if="record.bpmStatus === '1'">
                <a @click='startProcess(record)'>发起流程</a>
              </a-menu-item>
              <a-menu-item v-if="record.bpmStatus === '1' || record.bpmStatus === '4'">
                <a-popconfirm title='确定删除吗?' @confirm='() => handleDelete(record.id)'>
                  <a style='color:red'>删除</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item v-if="record.bpmStatus === '2'">
                  <a-popconfirm title='确定要作废流程吗?' @confirm='() => invalidProcess(record)'>
                    <a>作废流程</a>
                  </a-popconfirm>
              </a-menu-item>
              <a-menu-item v-if="record.bpmStatus !== '1'" @click='handleTrack(record)'>审批进度</a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
    <hf-pm-project-question-uprecords-list
      :mainId='selectedMainId'
      :dataType="'HfPmProjectQuestionUprecords'"
      ref='updateShow'
    />
    <hf-pm-project-question-uprecords-modal ref='modalReord'
                                            @ok='aftermodalReordOk'></hf-pm-project-question-uprecords-modal>
    <hf-pm-project-questions-modal ref='modalForm' @ok='modalFormOk'></hf-pm-project-questions-modal>
    <bpm-process-track-modal ref='bpmProcessTrackModal'></bpm-process-track-modal>

  </a-card>
</template>

<script>

import '@assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgBpmListMixin } from '@/mixins/JeecgBpmListMixin'
import HfPmProjectQuestionsModal from './modules/HfPmProjectQuestionsModal'
import { getAction, postAction } from '@api/manage'
import BpmStatusTagComponent from '@comp/bpm/bpmStatusTagComponent'
import BpmBizTaskDealModal from '@views/modules/bpmbiz/common/BpmBizTaskDealModal'
import BpmProcessTrackModal from '@views/modules/bpmbiz/common/BpmProcessTrackModal'
import { initDictOptions } from '@comp/dict/JDictSelectUtil'

export default {
  name: 'HfPmProjectQuestionsListTab',
  mixins: [JeecgBpmListMixin, mixinDevice],
  components: {
    HfPmProjectQuestionsModal,
    BpmStatusTagComponent,
    BpmBizTaskDealModal,
    BpmProcessTrackModal

  },
  data() {
    return {
      description: '项目问题tab页管理页面',
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '问题编号',
          align: 'center',
          dataIndex: 'questionCode',
          scopedSlots: { customRender: 'subjectName' }
        },
        {
          title: '专题名称',
          align: 'center',
          dataIndex: 'subjectName'
        },
        {
          title: '问题类型',
          align: 'center',
          dataIndex: 'questionType_dictText',
        },
        {
          title: '所属模块',
          align: 'center',
          dataIndex: 'beginDes'
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy'
        },
        {
          title: '负责人',
          align: 'center',
          dataIndex: 'dutyPersion'
        },
        {
          title: '预估人天',
          align: 'center',
          dataIndex: 'estimateWorkday'
        },
        {
          title: '期望时间',
          align: 'center',
          dataIndex: 'expectedTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '预计完成时间',
          align: 'center',
          dataIndex: 'endTime'
        },
        {
          title: '实际完成时间',
          align: 'center',
          dataIndex: 'confirmorTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '问题状态',
          align: 'center',
          dataIndex: 'questionState_dictText'
        },
        {
          title: '审批状态',
          align: 'center',
          dataIndex: 'bpmStatus_dictText',
          scopedSlots: { customRender: 'bpmStatus_dictText' }
        },
        // {
        //   title: '问题描述',
        //   align: 'center',
        //   dataIndex: 'questionDes',
        //   scopedSlots: { customRender: 'taskDes' }
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/pm/hfPmProjectQuestions/list',
        delete: '/pm/hfPmProjectQuestions/delete',
        deleteBatch: '/pm/hfPmProjectQuestions/deleteBatch',
        exportXlsUrl: '/pm/hfPmProjectQuestions/exportXls',
        importExcelUrl: 'pm/hfPmProjectQuestions/importExcel',
        confim: 'pm/hfPmProjectQuestions/confim',
        startProcess: '/act/process/extActProcess/startMutilProcess',
      },
      flowCode: 'dev_hf_pm_project_questions_001',
      selectedMainId: '',
      disableMixinCreated: true,
      queryModel:true
    }
  },
  created() {
  },
  props: {
    selectedLeftTreeData: {
      type: Object,
      default: () => {
      }
    }
  },
  watch: {
    selectedLeftTreeData: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val.selectedTreeType === 'project') {
          this.getListBySelectedKey(val.selectedKeys)
        } else if (val.selectedTreeType === 'subject') {
          this.getListBySelectedKey(val.selectedSubKeys)
        }
      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    handleAdd() {
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = '新增'
      this.$refs.modalForm.disableSubmit = false
      this.$refs.modalForm.defaultModel = this.selectedLeftTreeData
    },
    getListBySelectedKey(id) {
      if (this.selectedLeftTreeData.selectedTreeType === 'project') {
        this.propsParams = { projectId: id }
        this.loadData()
      } else if (this.selectedLeftTreeData.selectedTreeType === 'subject') {
        this.propsParams = { subjectId: id }
        this.loadData()
      }
    },
    startProcess(record) {
      this.$confirm({
        title: '提示',
        content: '确认提交流程吗?',
        onOk: () => {
          let params = {
            flowCode: this.flowCode,
            id: record.id,
            formUrl: 'hifar/pm/modules/HfPmProjectQuestionsForm',
            formUrlMobile: ''
          }
          postAction(this.url.startProcess, params)
            .then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.loadData()
                this.onClearSelected()
              } else {
                this.$message.warning(res.message)
              }
            })
            .catch((e) => {
              this.$message.warning('不识别的请求!')
            })
        }
      })
    },

    loadData() {
      let params = Object.assign({}, this.getQueryParams(), this.propsParams)
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
      }).finally(() => {
        this.loading = false
      })
    },
    aftermodalReordOk() {
      this.modalFormOk()
      this.getSum()
    },
    clickThenCheck(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    confimor(record) {
      if (record.confirmorName != null) {
        this.$message.error('此问题已确认，请勿重复确认!')
        return
      }
      var obj = this.$ls.storage.pro__Login_Username
      var jsonObj = JSON.parse(obj)
      record.confirmorName = jsonObj.value
      getAction(this.url.confim, record).then((res) => {
        if (res.success) {
          this.loadData()
          this.$message.success('操作完成。')
        } else {
          this.$message.warning('操作失败！')
        }
      })
    },
    handleEdit: function(record) {
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '编辑'
      this.$refs.modalForm.disableSubmit = false
    },
    handleDetail: function(record) {
      record.clickArchive = false //为了识别是点击了编辑或详情
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '详情'
      this.$refs.modalForm.disableSubmit = true
    },
    initDictConfig() {
      initDictOptions('bpm_status').then((res) => {
        if (res.success) {
          this.bpmStatusDictOptions = res.result
        }
      })
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>